/**
 * WebClient.js
 * @author Ulyan Sobin
 * @description WebClient class
 */

"use strict";

const { WebRequest, WebResponse } = require("./index");

class WebClient {

	/** 
	 * Request from web client
	 * @type {WebRequest}
	 */
	request;

	/**
	 * Response to web client
	 * @type {WebResponse}
	 */
	response;

	/**
	 * @private
	 * @type {Array.<string>}
	 */
	#pathStack;

	/**
	 * @type {string}
	 */
	current;

	/**
	 * Constructor of WebClient
	 * @param {WebRequest} request 
	 * @param {WebResponse} response 
	 */
	constructor (request, response) {
		this.request = request;
		this.response = response;
		
		this.#pathStack = [];
		for (let n of request.path)
			this.#pathStack.push(n);
	}

	/**
	 * Get next part of the URL's path
	 * @returns {string}
	 */
	routeNext() {
		this.current = this.#pathStack.shift();
		return this.current;
	}

	/**
	 * Peek next part of the URL's path
	 * @returns {string}
	 */
	peekNext() {
		return this.#pathStack[0];
	}

	/**
	 * Get remaining path.
	 * @returns {string}
	 */
	get path() {
		if (this.#pathStack.length == 0) return "";
		let result = "";
		for (let p of this.#pathStack) {
			result += '/';
			result += p;
		}
		return result;
	}
}

module.exports.WebClient = WebClient;